package com.hz.controller;

import com.alibaba.fastjson.JSONObject;
import com.hz.pojo.SafetyInfo;
import com.hz.pojo.User;
import com.hz.service.UserService;
import com.hz.service.impl.SafetyInfoServiceImpl;
import com.hz.service.impl.UserServiceImpl;
import com.hz.utils.MassageJsons;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/safetyInfo")
public class SafetyInfoController {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    SafetyInfoServiceImpl safetyInfoServiceImpl;
    @Resource
    UserServiceImpl userService;
    @RequestMapping("/upDaccountMoney")
    public MassageJsons updAccountMoney(String open_id,Double account_money){

        try {
            return safetyInfoServiceImpl.updAccountMoney(open_id,account_money);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MassageJsons("1","提现失败");
    }

    @RequestMapping("/getSafetyInfo")
    public MassageJsons getSafetyInfo(String open_id){
        SafetyInfo safetyInfo = null;
        try {
            safetyInfo = safetyInfoServiceImpl.getSafetyInfo(open_id);
            if(safetyInfo!=null){
                return new MassageJsons("0","请求成功");
            }else {
                return new MassageJsons("1","请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/updPayPassword")
    public MassageJsons updPayPassword(String open_id, String pay_password){
    //修改用户支付密码
        try {
            //判断是否修改成功
            if(safetyInfoServiceImpl.updPayPassword(open_id,pay_password)!=0){
                //成功之后查询最新用户信息
                User user = userService.getUserInfo(open_id);
                //存入缓存
                stringRedisTemplate.opsForValue().set(open_id,JSONObject.toJSONString(user));
                //成功后返回给前端
                return new MassageJsons("0","设置成功",user);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回失败信息
        return new MassageJsons("1","设置失败");
    }
}
